<template>
  <div class="RegisterComponent">
    <Navbar>
      <template #left>
        <div class="close-icon" @click="closeRegisterModal">
          <Icon name="zuojiantou" color="#ccc" size="18" />
        </div>
      </template>
    </Navbar>
    <form class="login-form">
      <h2>注册账号</h2>
      <div class="login-form-item">
        <input type="text" autocomplete="email" v-model="email" placeholder="请输入邮箱">
      </div>
      <div class="login-form-item">
        <input type="password" v-model="password" autocomplete="new-password" placeholder="请输入密码">
      </div>
      <div class="login-form-item">
        <input type="text" v-model="code" autocomplete="off" placeholder="输入邮箱验证码">
        <!-- 获取验证码-->
        <p class="get-code" @click="getCode(email)">获取验证码</p>
      </div>
      <div class="login-form-item">
        <button @click="register">注册</button>
      </div>
    </form>
  </div>
</template>

<script setup>
import Navbar from '@/components/common/Navbar.vue'
import Icon from '@/components/common/Icon.vue'
import { Toast } from '@/utils/showtoast'
import { ref } from 'vue'
// 自动处理 v-model
const model = defineModel({ default: false })

const closeRegisterModal = () => model.value = false

// 邮箱 密码 验证码
const email = ref('')
const password = ref('')
const code = ref('')
import { userGetCodeAPI, userRegisterAPI } from '@/api/login'
const getCode = async (email) => {
  if (!email) {
    console.error('请输入邮箱')
    return
  }
  try {
    const res = await userGetCodeAPI(email)
    console.log(res)
  } catch (error) {
    console.error(error.message)
  }
}
const register = async (event) => {
  // 禁止默认事件 提交表单时不刷新页面

  event.preventDefault()
  try {
    const res = await userRegisterAPI({
      email: email.value,
      password: password.value,
      code: code.value,
    })
    if (res.code === 200) {
      Toast('注册成功请登陆!', {
        position: 'bottom'
      })
      model.value = false
    } else {
      console.log('注册失败')
      Toast('该账号已注册', {
        position: 'bottom'
      })
      model.value = false
    }
  } catch (error) {
    ElMessage.error(error.message)
  }
}


</script>

<style lang="scss" scoped>
.RegisterComponent {
  @include fixed-top;
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  padding: 20px;
  z-index: 10001;

  .login-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 20px;
    border-radius: 10px;

    h2 {
      font-size: 20px;
      font-weight: bold;
      line-height: 1;
      margin-bottom: 20px;
    }

    .login-form-item {
      display: flex;
      flex-direction: column;
      gap: 5px;
      position: relative;

      .get-code {
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 14px;
        color: #879394;
        cursor: pointer;
        font-weight: bold;
      }

      input {
        padding: 10px;
        border-radius: 5px;
        background-color: #f9f9f9;
      }

      button {
        padding: 10px;
        border: 1px solid #ccc;
        border-radius: 5px;
        background-color: #007bff;
        color: #fff;
        cursor: pointer;
      }
    }
  }
}
</style>